package com.skywolf.chem.live.service.impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.sysconfig.pojo.dto.PageDTO;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.customer.entity.Signup;
import com.skywolf.chem.customer.param.SignupDto;
import com.skywolf.chem.customer.service.ISignupService;
import com.skywolf.chem.live.entity.Live;
import com.skywolf.chem.live.mapper.LiveMapper;
import com.skywolf.chem.live.param.LiveDto;
import com.skywolf.chem.live.param.LiveParam;
import com.skywolf.chem.live.param.VideoDto;
import com.skywolf.chem.live.service.ILiveService;
import com.skywolf.chem.manager.param.LiveQueryDto;
import com.skywolf.chem.manager.param.LiveSignUpQueryDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * <p>
 * 直播信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-11-30
 */
@Service
public class LiveServiceImpl extends ServiceImpl<LiveMapper, Live> implements ILiveService {

    @Autowired
    LiveMapper liveMapper;

    @Autowired
    ISignupService signupService;

    @Override
    public PageVO<VideoDto> pageLiveList(PageDTO queryParam) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<VideoDto> list = liveMapper.getLivePageList(startNum,endNum,1,null,null,null,null);
        Integer count = liveMapper.getLivePageCount(1,null,null,null,null);
        PageVO<VideoDto> pageVO = new PageVO(list,Long.valueOf(count),queryParam.getPageNum(),queryParam.getPageSize());
        return pageVO;
    }

    @Override
    public void enrollLive(LiveParam param, Long uid) throws ServiceException {
        Live live = liveMapper.selectById(param.getLiveId());
        if(live == null){
            throw  new ServiceException("直播资源不存在！");
        }
        LocalDateTime startTime = live.getSignTime();
        LocalDateTime endTime = live.getEndTime();
        if(startTime==null || endTime==null){
            throw  new ServiceException("不在报名时间内！");
        }
        if(System.currentTimeMillis() < Instant.from(startTime.atZone(ZoneId.systemDefault())).toEpochMilli()){
            throw  new ServiceException("报名还未开始！");
        }
        if(System.currentTimeMillis() > Instant.from(endTime.atZone(ZoneId.systemDefault())).toEpochMilli()){
            throw  new ServiceException("报名已结束！");
        }
        QueryWrapper<Signup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid).eq("live_id",param.getLiveId());
        Signup signup = signupService.getOne(queryWrapper);
        if(signup==null){
            signup = new Signup();
            BeanUtils.copyProperties(param,signup);
            signup.setUserId(uid);
            signupService.save(signup);
        }else{
            throw  new ServiceException("已经报过名啦！");
        }

    }

    @Override
    public PageVO<VideoDto> liveUser(PageDTO queryParam,Long uid) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<VideoDto> list = liveMapper.getLiveIngPageList(startNum,endNum,uid);
        Integer count = liveMapper.getLiveIngPageCount(uid);
        PageVO<VideoDto> pageVO = new PageVO(list,Long.valueOf(count),queryParam.getPageNum(),queryParam.getPageSize());
        return pageVO;
    }

    @Override
    public LiveDto getLiveInfo(Long liveId, Long uid) {
        return liveMapper.getLiveInfo(liveId,uid);
    }

    @Override
    public PageVO<VideoDto> pageLiveListManager(LiveQueryDto queryParam) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<VideoDto> list = liveMapper.getLivePageList(startNum,endNum,queryParam.getStatus(),queryParam.getTitle(),queryParam.getStartTime(),queryParam.getEndTime(),queryParam.getPlayStatus());
        Integer count = liveMapper.getLivePageCount(queryParam.getStatus(),queryParam.getTitle(),queryParam.getStartTime(),queryParam.getEndTime(),queryParam.getPlayStatus());
        PageVO<VideoDto> pageVO = new PageVO(list,Long.valueOf(count),queryParam.getPageNum(),queryParam.getPageSize());
        return pageVO;
    }

    @Override
    public PageVO<SignupDto> pageLiveSignUpListManager(LiveSignUpQueryDto queryParam) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<SignupDto> list = liveMapper.getLiveSignUpPageList(startNum,endNum,queryParam.getStatus(), queryParam.getTitle(),
                queryParam.getCompany(),queryParam.getPhone(),queryParam.getUserName(),queryParam.getStartTime(),queryParam.getEndTime());
        Integer count = liveMapper.getLiveSignUpPageCount(queryParam.getStatus(), queryParam.getTitle(),
                queryParam.getCompany(),queryParam.getPhone(),queryParam.getUserName(),queryParam.getStartTime(),queryParam.getEndTime());
        PageVO<SignupDto> pageVO = new PageVO(list,Long.valueOf(count),queryParam.getPageNum(),queryParam.getPageSize());
        return pageVO;
    }

    @Override
    public List<Live> getGroupInfo(List<String> groupIds) {
        return liveMapper.getGroupInfo(groupIds);
    }
}
